package cn.uniquetech.hky;

import java.util.List;

public class GenViewUtil {

	private static String htmlTailStr = "\r\n";
	private static String key = "iid";
	// 要排除的字段
	private static String fields = ",iid,icreator,dcreator,ccreator,imodify,dmodify,cmodify,";
	private static String tableFields = ",iid,icreator,imodify,";

	private static String parseIndex(String tableName, String voName, String tableRemarks, List<ColumnVO> columnList) {

		String packageName = GenerateCodeMysql.PACKAGENAME;
		String clazz = voName;
		String lowClazz = clazz.toLowerCase();

		StringBuffer sb = new StringBuffer();
		sb.append("<!DOCTYPE html>" + htmlTailStr);
		sb.append("<html lang=\"zh-cn\" xmlns:th=\"http://www.thymeleaf.org\">" + htmlTailStr);
		sb.append("<head>" + htmlTailStr);
		sb.append("<meta charset=\"utf-8\" />" + htmlTailStr);
		sb.append("<title>" + tableRemarks + "</title>" + htmlTailStr);
		sb.append("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\" />" + htmlTailStr);
		sb.append("<link th:href=\"@{../assets/css/page.css}\" rel=\"stylesheet\" />" + htmlTailStr);
		sb.append("<!--[if lt IE 9]>" + htmlTailStr);
		sb.append("\t<script th:src=\"@{../assets/js/html5shiv.js}\"></script>" + htmlTailStr);
		sb.append("\t<script th:src=\"@{../assets/js/respond.min.js}\"></script>" + htmlTailStr);
		sb.append("<![endif]-->" + htmlTailStr);
		sb.append("</head>" + htmlTailStr);

		sb.append("<body class=\"inside-header inside-aside \">" + htmlTailStr);
		sb.append("<div id=\"main\" role=\"main\">" + htmlTailStr);
		sb.append("<div class=\"tab-content tab-addtabs\">" + htmlTailStr);
		sb.append("<div id=\"content\">" + htmlTailStr);
		sb.append("<div class=\"row\">" + htmlTailStr);
		sb.append("<div class=\"col-xs-12 col-sm-12 col-md-12 col-lg-12\">" + htmlTailStr);
		sb.append("<div class=\"content\">" + htmlTailStr);
		sb.append("<div class=\"panel panel-default panel-intro\">" + htmlTailStr);
		sb.append("<div class=\"panel-body\">" + htmlTailStr);
		sb.append("<div id=\"myTabContent\" class=\"tab-content\">" + htmlTailStr);
		sb.append("<div class=\"tab-pane fade active in\" id=\"one\">" + htmlTailStr);
		sb.append("<div class=\"widget-body no-padding\">" + htmlTailStr);
		sb.append("<div id=\"toolbar\" class=\"toolbar\">" + htmlTailStr);
		// sb.append("<a href=\"javascript:;\" class=\"btn btn-primary btn-refresh\"><i
		// class=\"fa fa-refresh\"></i> </a>" + htmlTailStr);
		sb.append("<a id=\"btn-add\" href=\"javascript:onAddItem();\" class=\"btn btn-success btn-add\"><i class=\"fa fa-plus\"></i> 新增</a> " + htmlTailStr);
		sb.append("<a id=\"btn-edit\" href=\"javascript:onEditItem();\" class=\"btn btn-success btn-edit\"><i class=\"fa fa-pencil\"></i> 修改</a> " + htmlTailStr);
		sb.append("<a href=\"javascript:onDeleteItem();\" class=\"btn btn-danger btn-del\"><i class=\"fa fa-trash\"></i> 删除</a>" + htmlTailStr);
		sb.append("</div>" + htmlTailStr);
		sb.append(
				"<table id=\"table\" data-toggle=\"table\" class=\"table table-striped table-bordered table-hover\" style=\"width:100%\"  data-method=\"get\" data-toolbar=\"#toolbar\" data-search=\"true\" data-single-select=\"false\" data-search-text=\"请输入查询内容\" data-show-refresh=\"true\" data-show-export=\"true\" data-side-pagination=\"server\" data-minimum-count-columns=\"2\" data-pagination=\"true\" data-id-field=\"iid\" data-unique-id=\"iid\" data-click-to-select=\"true\" data-page-list=\"[10, 15,20,30, 60, 80]\" data-url=\"./pageQuery\" data-show-footer=\"false\">"
						+ htmlTailStr);
		sb.append("<thead>" + htmlTailStr);
		sb.append("<tr>" + htmlTailStr);

		sb.append("<th data-field=\"state\" data-checkbox=\"true\">state</th>" + htmlTailStr);
		sb.append("<th data-field=\"iid\"   data-formatter=\"indexFormatter\" data-width=\"50px\" data-align=\"center\">序号</th>  " + htmlTailStr);

		for (ColumnVO columnVO : columnList) {
			if (tableFields.indexOf(columnVO.getColName()) == -1) {
				sb.append("<th data-field=\"" + columnVO.getColName() + "\">" + columnVO.getColRemarks() + "</th>" + htmlTailStr);
			}
		}

		sb.append("</tr>" + htmlTailStr);
		sb.append("</thead>" + htmlTailStr);
		sb.append("</table>" + htmlTailStr);
		sb.append("</div></div></div></div></div></div></div></div></div></div></div>" + htmlTailStr);
		sb.append("<script type=\"text/javascript\" th:src=\"@{../assets/libs/jquery/dist/jquery.js}\"></script>" + htmlTailStr);
		sb.append("<script type=\"text/javascript\" th:src=\"@{../assets/libs/bootstrap/dist/js/bootstrap.js}\"></script>" + htmlTailStr);
		sb.append("<script type=\"text/javascript\" th:src=\"@{../assets/libs/toastr/toastr.js}\"></script>" + htmlTailStr);
		sb.append("<script type=\"text/javascript\" th:src=\"@{../assets/libs/bootstrap-table/dist/bootstrap-table.min.js}\"></script>" + htmlTailStr);
		sb.append("<script type=\"text/javascript\" th:src=\"@{../assets/libs/bootstrap-table/dist/locale/bootstrap-table-zh-CN.js}\"></script>" + htmlTailStr);
		sb.append("<script type=\"text/javascript\" th:src=\"@{../assets/libs/bootstrap-table/dist/extensions/export/bootstrap-table-export.js}\"></script>" + htmlTailStr);
		sb.append("<script type=\"text/javascript\" th:src=\"@{../assets/libs/layer/src/layer.js}\"></script>" + htmlTailStr);
		sb.append("<script type=\"text/javascript\" th:src=\"@{../assets/js/page.js}\"></script>" + htmlTailStr);
		sb.append("</body></html>" + htmlTailStr);

		String servicePackage = packageName + ".view." + lowClazz;
		FileUtil.writeFile(servicePackage, "index.html", sb.toString());
		return sb.toString();
	}

	private static String parseEdit(String tableName, String voName, String tableRemarks, List<ColumnVO> columnList) {
		String packageName = GenerateCodeMysql.PACKAGENAME;
		String clazz = voName;
		String lowClazz = clazz.toLowerCase();
		StringBuffer sb = new StringBuffer();
		
		
		sb.append("<!DOCTYPE html>" + htmlTailStr);
		sb.append("<html lang=\"zh-cn\" xmlns:th=\"http://www.thymeleaf.org\">" + htmlTailStr);
		sb.append("<head>" + htmlTailStr);
		sb.append("<meta charset=\"utf-8\" />" + htmlTailStr);
		sb.append("<title>" + tableRemarks + "</title>" + htmlTailStr);
		sb.append("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\" />" + htmlTailStr);
		sb.append("<link th:href=\"@{../assets/css/page.css}\" rel=\"stylesheet\" />" + htmlTailStr);
		sb.append("<!--[if lt IE 9]>" + htmlTailStr);
		sb.append("\t<script th:src=\"@{../assets/js/html5shiv.js}\"></script>" + htmlTailStr);
		sb.append("\t<script th:src=\"@{../assets/js/respond.min.js}\"></script>" + htmlTailStr);
		sb.append("<![endif]-->" + htmlTailStr);
		sb.append("</head>" + htmlTailStr);
		
		sb.append("<body class=\"inside-header inside-aside is-dialog\">\r\n" + 
				"	<div id=\"main\" role=\"main\">\r\n" + 
				"		<div class=\"tab-content tab-addtabs\">\r\n" + 
				"			<div id=\"content\">\r\n" + 
				"				<div class=\"row\">\r\n" + 
				"					<div class=\"col-xs-12 col-sm-12 col-md-12 col-lg-12\">\r\n" + 
				"						<div class=\"content\">"+htmlTailStr);
		
		sb.append("<form id=\"edit-form\" class=\"form-horizontal form-ajax\" role=\"form\" data-toggle=\"validator\" method=\"POST\" action=\"./edit\">"+htmlTailStr);
		sb.append("<input id=\"iid\" name=\"iid\" type=\"hidden\"/>"+htmlTailStr);
		for(ColumnVO columnVO : columnList) {
			if(fields.indexOf(columnVO.getColName()) !=-1) {
				continue;
			}
			sb.append("<div class=\"form-group\">\r\n" + 
					"									<label for=\"cloginpwd\" class=\"control-label col-xs-12 col-sm-2\">"+columnVO.getColRemarks()+":</label>\r\n" + 
					"									<div class=\"col-xs-12 col-sm-8\">\r\n" + 
					"										<input type=\"text\" class=\"form-control\" id=\""+columnVO.getColName()+"\" name=\""+columnVO.getColName()+"\" value=\"\" data-rule=\"required\" />\r\n" + 
					"									</div>\r\n" + 
					"								</div>"+htmlTailStr);
		}
		
		sb.append("<div class=\"form-group\">\r\n" + 
				"									<label class=\"control-label col-xs-12 col-sm-2\"></label>\r\n" + 
				"									<div class=\"col-xs-12 col-sm-8 text-center\">\r\n" + 
				"										<button type=\"submit\" class=\"btn btn-success btn-embossed\">确定</button>\r\n" + 
				"										<button type=\"reset\" class=\"btn btn-default btn-embossed\">重置</button>\r\n" + 
				"									</div>\r\n" + 
				"								</div>"+htmlTailStr);
		sb.append("</form>"+htmlTailStr);
		sb.append("						</div>\r\n" + 
				"					</div>\r\n" + 
				"				</div>\r\n" + 
				"			</div>\r\n" + 
				"		</div>\r\n" + 
				"	</div>\r\n" + 
				"	<script type=\"text/javascript\" th:src=\"@{../assets/libs/jquery/dist/jquery.js}\"></script>\r\n" + 
				"	<script type=\"text/javascript\" th:src=\"@{../assets/libs/bootstrap/dist/js/bootstrap.js}\"></script>\r\n" + 
				"	<script type=\"text/javascript\" th:src=\"@{../assets/libs/toastr/toastr.js}\"></script>\r\n" + 
				"	<script type=\"text/javascript\" th:src=\"@{../assets/libs/layer/src/layer.js}\"></script>\r\n" + 
				"	<script type=\"text/javascript\" th:src=\"@{../assets/libs/bootstrap-datetimepicker/js/bootstrap-datetimepicker.min.js}\"></script>\r\n" + 
				"	<script type=\"text/javascript\" th:src=\"@{../assets/libs/bootstrap-datetimepicker/js/locales/bootstrap-datetimepicker.zh-CN.js}\"></script>\r\n" + 
				"</body>\r\n" + 
				"</html>"+htmlTailStr);
		

		String servicePackage = packageName + ".view." + lowClazz;
		FileUtil.writeFile(servicePackage, "edit.html", sb.toString());
		return sb.toString();
	}

	public static void parseView(String tableName, String voName, String tableRemarks, List<ColumnVO> columnList) {
		parseIndex(tableName, voName, tableRemarks, columnList);
		parseEdit(tableName, voName, tableRemarks, columnList);
	}
}
